package com.ch.common.base.activity

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.ch.common.base.viewModel.BaseLibViewModel

abstract class BaseLibActivity<VM : BaseLibViewModel, DB : ViewDataBinding> : AppCompatActivity() {
    protected lateinit var mBinding: DB;
    protected lateinit var mContext: Context
    protected abstract val viewModel: VM

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mContext = this
        initObserve()
    }

    override fun setContentView(layoutResID: Int) {
        mBinding = DataBindingUtil.inflate(
            LayoutInflater.from(mContext),
            layoutResID, null, false
        )
        // 绑定生命周期管理
        mBinding.lifecycleOwner = this
        // 绑定 ViewModel
        bindViewModel()
        super.setContentView(mBinding.root)
    }



    /**
     * TODO 必须把viewModel与DataBinding绑定起来，不然数据为空，无作用
     * */
    abstract fun bindViewModel()

    abstract fun initObserve()


}